package com.atguigu.rabbitmq.three;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.atguigu.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;

/**
 * ClassName: Work04
 * Description: 消息在手动应答时不丢失，放回队列中重新消费
 * Date: 2023/04/27 PM 4:23:25
 * <p>
 * project: atguigu-rabbitmq
 * package: com.atguigu.rabbitmq.three
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class Work04 {

    /**
     * 队列名称
     */
    public static final String TASK_QUEUE_NAME = "ack_queue";

    /**
     * 接受消息
     */
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C2等待接收消息处理时间较长");

        // 设置不公平分发
        // int prefetchCount = 1;
        // 预取值是5
        int prefetchCount = 5;
        channel.basicQos(prefetchCount);
        // 采用手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, (consumerTag, message) -> {
            // 沉睡1S
            SleepUtils.sleep(30);
            System.out.println("接收到的消息:" + new String(message.getBody(), StandardCharsets.UTF_8));
            // 手动应答
            /*
              1.消息的标记 tag
              2.是否批量应答:false 不批量应答信道中的消息   true:批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        }, consumerTag -> System.out.println(consumerTag + "消费者取消消费接口回调逻辑"));

    }
}
